<?php
App::import('Vendor', 'php-excel-reader/excel_reader2');
class EventsController extends AppController{
		var $name = 'Events';
	var $uses = array('Event','UnionMember','Union');
	var $components = array('RequestHandler', 'Filter', 'FileUpload');
	var $helpers = array('Js' => array('Jquery'), 'xls');
	var $paginate = array('limit' => 5,'order' => array('Event.id' => 'asc'));
 	function index(){
    	$this->set('list_union',$this->Union->find('list', array('conditions' => array('Union.active' => '1'))));
    	$filter = $this->Filter->process($this);
    	$this->set('events', $this->paginate('Event', $filter));
    	$this->set('isFilter', $filter);    	
    }
	function exportXls(){
		$filter = $this->Filter->process($this);
		$this->set('events', $this->Event->find('all', array('conditions'=>$filter)));
	}
	function view($id) {
	$this->Event->id = $id;
	$this->set('event_info', $this->Event->read());
	}
    function add(){
    	$this->Event->create();
    	$this->set('list_event',$this->Event->find('list'));
    	$this->set('last_event_id',$this->Event->find('count'));
    	$this->set('list_status', array('1'=>'Đang diễn ra','0'=>'Kết thúc'));
    	$this->set('list_union',$this->Union->find('list', array('conditions' => array('Union.active' => '1'))));
    	if(!empty($this->data)){
    		if($this->Event->save($this->data)){
    			$this->flash(__('Thao tác thành công.',true),array('controller'=>'Events','action' => 'index'));    	
    		}
    		else{
    			$this->flash(__('Thao tác thất bại. Vui lòng kiểm tra lại!',true), array('action' => 'index'));
    		}
    	}
    }
    function edit($id){
    	$this->Event->id = $id;
    	$this->set('list_event',$this->Event->find('list'));    
    	$this->set('list_status', array('1'=>'Đang diễn ra','0'=>'Kết thúc'));	
    	$this->set('list_union',$this->Union->find('list', array('conditions' => array('Union.active' => '1'))));
    	if (empty($this->data)) {
    		$this->data = $this->Event->read();    		
    	} else {
    		if ($this->Event->save($this->data)) {
    			$this->flash(__('Thao tác thành công.', true), array('action' => 'index'));
    		}
    		else
    		$this->flash(__('Thao tác thất bại. Vui lòng kiểm tra lại.', true), array('action' => 'index'));
    	}
    }
    function delete($id){
    	$this->Event->delete($id);
    	$this->redirect(array('action'=>'index'));
    }
    function delMulti(){
    	if($this->data['SelectedEs']){
    		$listSelect = array();
    		foreach ($this->data['SelectedEs'] as $key => $value){
    			if($value != '0'){
    				array_push($listSelect, $value);
    				
    			}    			
    		}
    		$this->set('events', $this->Event->find('all', array('conditions'=>array('Event.id'=>$listSelect))));
    		$this->Session->write('listDel', $listSelect);
    	}
    	else{
    		if($this->Session->check('listDel')){
    			foreach ($this->Session->read('listDel') as $value){    				
    				$this->Event->delete($value);
    			}
    			$this->Session->delete('listDel');
				$this->flash(__('Xóa thành công.', true), array('action' => 'index'));
    		}
    		else{
    			$this->flash(__('Thao tác thất bại. Vui lòng kiểm tra lại.', true), array('action' => 'index'));
    		}
    	}
    	
    }
}
?>